package service.role

import entity.Role
import repository.RoleRepository
import repository.RoleRepositoryImpl
import service.role.RoleService

class RoleServiceImpl : RoleService {
    private val roleRepository: RoleRepository = RoleRepositoryImpl()
    override fun getRole(level: Int): Role? {
        return roleRepository.findById(level).let {
            if (it.isPresent) {
                it.get()
            } else {
                null
            }
        }
    }

    override fun getRole(roleType: Role.RoleType): Role {
        return roleRepository.findByName(roleType)
    }

    override fun getAll(): List<Role?> = roleRepository.findAll()

    override fun save(role: Role): Role = roleRepository.save(role)

    override fun update(role: Role): Role = roleRepository.save(role)

    override fun delete(level: Int) = roleRepository.findById(level).let {
        if (it.isPresent) {
            roleRepository.delete(it.get())

        }
    }
}